package com.tensquare.sms.listener;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.tensquare.sms.utils.SMSUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Macross
 * @Type SMSListener.java
 * @Desc SMS监听器
 * @date 2019/12/23 11:34
 */
@RabbitListener(queues = "sms")
@Component
public class SMSListener {

    @Autowired
    private SMSUtil smsUtil;

    /**
     * 短信签名
     */
    private final String SING_NAME = "品优购购物平台";
    /**
     * 短信模板code
     */
    private final String VALID_CODE_TEMPLATECODE = "SMS_178761247";

    /**
     * 接受到消息后的处理方法
     * @param dataMap 生产者发送的数据
     */
    @RabbitHandler
    public void sendMessage(Map<String,String> dataMap){
        try {
            //获取手机号
            String mobile = dataMap.get("mobile");
            //获取验证码
            String code = dataMap.get("code");
            //生成一个存放验证码的map
            Map<String,String> paramMap = new HashMap(1);
            //存放验证码
            paramMap.put("code",code);
            //将map转换为json字符串
            String param = JSON.toJSONString(paramMap);
            //发送短信
            smsUtil.sendSms(mobile,SING_NAME,VALID_CODE_TEMPLATECODE,param);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
